<?php
namespace cms\modules\v1\controllers;

use cms\modules\v1\models\access\ModuleActions;
use cms\modules\v1\models\im\ImCircle;
use cms\modules\v1\models\im\ImFriend;
use cms\modules\v1\models\im\ImGroup;
use cms\modules\v1\models\im\ImGroupUser;
use cms\modules\v1\models\user\UserAddress;
use common\models\Functions;
use Yii;
use yii\base\BaseObject;
use yii\filters\auth\HttpHeaderAuth;
use yii\helpers\ArrayHelper;

/**
 * Default controller for the `v1` module
 */
class ImController extends DefaultController
{

    public $modelClass = 'cms\modules\v1\models\im\ImUserLevel';

    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            'authenticator' => [
                'class' =>  HttpHeaderAuth::className(),    //head头部验证
                'header' => 'access-token',
                'optional' => [

                ]
            ]
        ]);
    }


    /* ====================== 好友关系 ========================= */
    //用户地址列表
    public function actionFriendList($page = 1, $size = 20){
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new ImFriend())->getListData($page, $size);
        }
    }


    /* ====================== 群管理 ========================= */
    //列表
    public function actionGroupList($page = 1, $size = 20){
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isGet) {
            return (new ImGroup())->getListData($page, $size);
        }
    }

    //切换启用禁用状态
    public function actionGroupSwitchStatus() {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isPost) {
            return (new ImGroup())->switchStatusGroup();
        }
    }

    //解散群
    public function actionGroupCancel($id = 0) {
        $check = (new ModuleActions())->checkActions();
        if ($check !== true) {
            return Functions::formatRestAPI(1, $check);
        }
        if (Yii::$app->request->isDelete) {
            return (new ImGroup())->cancelGroup($id);
        }
    }



    /* ====================== 群成员管理 ========================= */
    //保证金列表
    public function actionGroupUserList($page = 1, $size = 20){
        if (Yii::$app->request->isGet) {
            return (new ImGroupUser())->getListData($page, $size);
        }
    }

}
